package com.chen.rabbitMQ.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class DirectConfig {

    /**
     * 声明普通判题时的交换机
     *
     * @return Direct类型交换机
     */
    @Bean(name = "judgeDirectExchange")
    public DirectExchange judgeDirectExchange() {
        DirectExchange exchange = null;
        try {
            exchange = ExchangeBuilder.directExchange("Judge.Direct").build();
            log.info("普通判题时的交换机声明成功");
        } catch (Exception e) {
            log.error("普通判题时的交换机声明失败: " + e.getMessage());
        }
        return exchange;
    }

    /**
     * 创建普通判题时的队列
     *
     * @return
     */
    @Bean(name = "judgeDirectQueue")
    public Queue judgeDirectQueue() {
        Queue queue = null;
        try {
            queue = new Queue("Direct.JudgeQueue");
            log.info("普通判题时的队列创建成功");
        } catch (Exception e) {
            log.error("普通判题时的队列创建失败: " + e.getMessage());
        }
        return queue;
    }

    /**
     * 绑定普通判题时的队列和交换机
     */
    @Bean
    public Binding bindingQueueWithJudge(@Qualifier("judgeDirectQueue") Queue directQueue, @Qualifier("judgeDirectExchange") DirectExchange directExchange) {
        try {
            Binding binding = BindingBuilder.bind(directQueue).to(directExchange).with("Judge");
            log.info("绑定普通判题时的队列和交换机成功");
            return binding;
        } catch (Exception e) {
            log.error("创建绑定失败" + e);
            return null;
        }
    }


    /**
     * 声明比赛时的交换机
     *
     * @return
     */
    @Bean(name = "contestDirectExchange")
    public DirectExchange contestDirectExchange() {
        DirectExchange exchange = null;
        try {
            exchange = ExchangeBuilder.directExchange("Contest.Direct").build();
            log.info("比赛时的交换机声明成功");
        } catch (Exception e) {
            log.error("比赛时的交换机声明失败: " + e.getMessage());
        }
        return exchange;
    }

    /**
     * 声明比赛时的队列
     *
     * @return
     */
    @Bean(name = "contestDirectQueue")
    public Queue contestDirectQueue() {
        Queue queue = null;
        try {
            queue = new Queue("Direct.ContestQueue");
            log.info("比赛时的队列创建成功");
        } catch (Exception e) {
            log.error("比赛时的队列创建失败: " + e.getMessage());
        }
        return queue;
    }

    /**
     * 绑定比赛时的队列和交换机
     *
     * @return
     */
    @Bean
    public Binding bindingQueueWithContest(@Qualifier("contestDirectQueue") Queue directQueue, @Qualifier("contestDirectExchange") DirectExchange directExchange) {
        try {
            Binding binding = BindingBuilder.bind(directQueue).to(directExchange).with("Contest");
            log.info("绑定比赛时的队列和交换机成功");
            return binding;
        } catch (Exception e) {
            log.error("创建绑定失败", e);
            return null;
        }
    }


//    @Bean
//    public MessageConverter messageConverter(){
//        // 1.定义消息转换器
//        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
//        // 2.配置自动创建消息id，用于识别不同消息，也可以在业务中基于ID判断是否是重复消息
//        jackson2JsonMessageConverter.setCreateMessageIds(true);
//        return jackson2JsonMessageConverter;
//    }
}